package com.mj.haier.calc.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.calc.domain.AzFinSettleCarsteam;
import com.mj.haier.calc.repository.AzFinSettleCarsteamRepository;
import com.mj.haier.calc.repository.search.AzFinSettleCarsteamSearchRepository;
import com.mj.haier.calc.web.rest.errors.BadRequestAlertException;
import com.mj.haier.calc.web.rest.util.HeaderUtil;
import com.mj.haier.calc.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing AzFinSettleCarsteam.
 */
@RestController
@RequestMapping("/api")
public class AzFinSettleCarsteamResource {

    private final Logger log = LoggerFactory.getLogger(AzFinSettleCarsteamResource.class);

    private static final String ENTITY_NAME = "microCalcAzFinSettleCarsteam";

    private final AzFinSettleCarsteamRepository azFinSettleCarsteamRepository;

    private final AzFinSettleCarsteamSearchRepository azFinSettleCarsteamSearchRepository;

    public AzFinSettleCarsteamResource(AzFinSettleCarsteamRepository azFinSettleCarsteamRepository, AzFinSettleCarsteamSearchRepository azFinSettleCarsteamSearchRepository) {
        this.azFinSettleCarsteamRepository = azFinSettleCarsteamRepository;
        this.azFinSettleCarsteamSearchRepository = azFinSettleCarsteamSearchRepository;
    }

    /**
     * POST  /az-fin-settle-carsteams : Create a new azFinSettleCarsteam.
     *
     * @param azFinSettleCarsteam the azFinSettleCarsteam to create
     * @return the ResponseEntity with status 201 (Created) and with body the new azFinSettleCarsteam, or with status 400 (Bad Request) if the azFinSettleCarsteam has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/az-fin-settle-carsteams")
    @Timed
    public ResponseEntity<AzFinSettleCarsteam> createAzFinSettleCarsteam(@Valid @RequestBody AzFinSettleCarsteam azFinSettleCarsteam) throws URISyntaxException {
        log.debug("REST request to save AzFinSettleCarsteam : {}", azFinSettleCarsteam);
        if (azFinSettleCarsteam.getId() != null) {
            throw new BadRequestAlertException("A new azFinSettleCarsteam cannot already have an ID", ENTITY_NAME, "idexists");
        }
        AzFinSettleCarsteam result = azFinSettleCarsteamRepository.save(azFinSettleCarsteam);
        azFinSettleCarsteamSearchRepository.save(result);
        return ResponseEntity.created(new URI("/api/az-fin-settle-carsteams/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /az-fin-settle-carsteams : Updates an existing azFinSettleCarsteam.
     *
     * @param azFinSettleCarsteam the azFinSettleCarsteam to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated azFinSettleCarsteam,
     * or with status 400 (Bad Request) if the azFinSettleCarsteam is not valid,
     * or with status 500 (Internal Server Error) if the azFinSettleCarsteam couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/az-fin-settle-carsteams")
    @Timed
    public ResponseEntity<AzFinSettleCarsteam> updateAzFinSettleCarsteam(@Valid @RequestBody AzFinSettleCarsteam azFinSettleCarsteam) throws URISyntaxException {
        log.debug("REST request to update AzFinSettleCarsteam : {}", azFinSettleCarsteam);
        if (azFinSettleCarsteam.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        AzFinSettleCarsteam result = azFinSettleCarsteamRepository.save(azFinSettleCarsteam);
        azFinSettleCarsteamSearchRepository.save(result);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, azFinSettleCarsteam.getId().toString()))
            .body(result);
    }

    /**
     * GET  /az-fin-settle-carsteams : get all the azFinSettleCarsteams.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of azFinSettleCarsteams in body
     */
    @GetMapping("/az-fin-settle-carsteams")
    @Timed
    public ResponseEntity<List<AzFinSettleCarsteam>> getAllAzFinSettleCarsteams(Pageable pageable) {
        log.debug("REST request to get a page of AzFinSettleCarsteams");
        Page<AzFinSettleCarsteam> page = azFinSettleCarsteamRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/az-fin-settle-carsteams");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /az-fin-settle-carsteams/:id : get the "id" azFinSettleCarsteam.
     *
     * @param id the id of the azFinSettleCarsteam to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the azFinSettleCarsteam, or with status 404 (Not Found)
     */
    @GetMapping("/az-fin-settle-carsteams/{id}")
    @Timed
    public ResponseEntity<AzFinSettleCarsteam> getAzFinSettleCarsteam(@PathVariable Long id) {
        log.debug("REST request to get AzFinSettleCarsteam : {}", id);
        Optional<AzFinSettleCarsteam> azFinSettleCarsteam = azFinSettleCarsteamRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(azFinSettleCarsteam);
    }

    /**
     * DELETE  /az-fin-settle-carsteams/:id : delete the "id" azFinSettleCarsteam.
     *
     * @param id the id of the azFinSettleCarsteam to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/az-fin-settle-carsteams/{id}")
    @Timed
    public ResponseEntity<Void> deleteAzFinSettleCarsteam(@PathVariable Long id) {
        log.debug("REST request to delete AzFinSettleCarsteam : {}", id);

        azFinSettleCarsteamRepository.deleteById(id);
        azFinSettleCarsteamSearchRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/az-fin-settle-carsteams?query=:query : search for the azFinSettleCarsteam corresponding
     * to the query.
     *
     * @param query the query of the azFinSettleCarsteam search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/az-fin-settle-carsteams")
    @Timed
    public ResponseEntity<List<AzFinSettleCarsteam>> searchAzFinSettleCarsteams(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of AzFinSettleCarsteams for query {}", query);
        Page<AzFinSettleCarsteam> page = azFinSettleCarsteamSearchRepository.search(queryStringQuery(query), pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/az-fin-settle-carsteams");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
